package com.emma.interceptor;

import com.emma.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            System.out.println("拦截了非法用户"+request.getRemoteAddr());
            response.sendRedirect(request.getContextPath()+"/login.html");
            return false;

        }else {
            System.out.println("合法,放行用户"+request.getRemoteAddr());
            return true;
        }

    }
}
